﻿Imports System.IO
'下面的类表示自身含有地理信息的图片，比如dem,tiff等
<Serializable()> Public Class IMGGEOFileLayer
    Inherits ImageLayer

    Public Sub New(ByVal ln As String, ByVal fn As String)
        MyBase.New(ln, fn)


        Me.isUseTran = False

    End Sub

    '这些都是为了讨厌的VB事件逻辑啊
    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub



    Public Overrides Sub InitNewLayer(ByRef m As AxMapWinGIS.AxMap, ByVal OutPath As String)

        Dim str As String = System.IO.Path.GetFileName(fname)

        System.IO.File.Copy(fname, OutPath + str, True)

        fname = OutPath + str

        InitLayer(m)

    End Sub

    Public Overrides Sub SaveAs(ByVal strPath As String)

      
        File.Copy(fname, strPath + Path.GetFileName(fname), True)


    End Sub

    Public Overrides Sub ReLoad(ByRef m As AxMapWinGIS.AxMap, ByVal fPath As String)

        fname = fPath + Path.GetFileName(fname)

        Me.InitLayer(m)


    End Sub

    Public Overrides Sub InitLayer(ByRef m As AxMapWinGIS.AxMap)
        mapwin = m



        Dim img As New MapWinGIS.Image
        img.Open(fname)

        img.ImageColorScheme = MapWinGIS.PredefinedColorScheme.SummerMountains

        m_iLayer = mapwin.AddLayer(img, True)

        mapwin.set_LayerKey(m_iLayer, lname)


    End Sub

    Public Sub ChangeImage(ByVal fn As String)
        '去掉原有的层,但要先保存一些变量
        Dim posOld As Integer

        posOld = mapwin.get_LayerPosition(m_iLayer)


        '先这样用吧
        Me.Dispose()
        mapwin.RemoveLayer(m_iLayer)

        '删除旧有文件

        If System.IO.File.Exists(fname) Then

            File.SetAttributes(fname, FileAttributes.Normal)
            System.IO.File.Delete(fname)

        End If


        '粘贴新的图片

        Dim svPath As String = Path.GetDirectoryName(fname) + "\"

        File.Copy(fn, svPath + Path.GetFileName(fn), True)

        fname = svPath + Path.GetFileName(fn)


        '添加到图层

        Dim img As New MapWinGIS.Image

        Dim b As Boolean = img.Open(fname)

        If Not b Then

            System.Windows.Forms.MessageBox.Show("打开文件错误  " + fname)
        End If


        m_iLayer = mapwin.AddLayer(img, m_ShowLayer)
        mapwin.set_LayerKey(m_iLayer, lname)



        '层位置信息校正

        Dim posNew As Integer = mapwin.get_LayerPosition(m_iLayer)

        Dim posAdd As Integer = posNew - posOld

        If posAdd = 0 Then
            Return

        End If

        For i As Integer = 1 To posAdd

            mapwin.MoveLayerDown(posNew)
            posNew = posNew - 1



        Next




    End Sub

End Class
